package com.dfec.water.dao;

import com.dfec.framework.mybatis.core.mapper.BaseMapperX;
import com.dfec.framework.mybatis.core.page.PageParam;
import com.dfec.framework.mybatis.core.page.PageUtils;
import com.dfec.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.dfec.water.entity.YdbAreaDeviceEntity;
import com.dfec.water.entity.YdbDeviceTypeDictEntity;
import com.dfec.water.entity.YdbDeviceTypeEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 区域和设备关联信息Dao接口
 *
 * @author df
 * @date Tue Jan 14 11:12:56 CST 2025
 */
@Mapper
public interface YdbAreaDeviceDao extends BaseMapperX<YdbAreaDeviceEntity> {

    /**
     * description:分页查询
     *
     * @param pageParam 分页参数
     * @return
     * @author trg
     * @date 2023/6/27 15:56
     */
    default PageUtils selectPage(PageParam<YdbAreaDeviceEntity> pageParam) {
        YdbAreaDeviceEntity entity = pageParam.getEntity() == null ? new YdbAreaDeviceEntity() :pageParam.getEntity();
        return this.selectPage(pageParam, dealParams(entity));
    }


    /**
     * 不带分页查询
     * @param ydbAreaDeviceEntity
     * @return
     */
    default  List<YdbAreaDeviceEntity> selectAll(YdbAreaDeviceEntity ydbAreaDeviceEntity){
        return this.selectList(dealParams(ydbAreaDeviceEntity));
    }

    /**
     * description:查询条件拼接
     *
     * @param entity 参数
     * @return
     * @author trg
     * @date 2023/6/27 15:56
     */
     default LambdaQueryWrapperX<YdbAreaDeviceEntity> dealParams(YdbAreaDeviceEntity entity){
        return new LambdaQueryWrapperX<YdbAreaDeviceEntity>()
                .eqIfPresent(YdbAreaDeviceEntity::getAreaId, entity.getAreaId())
                .eqIfPresent(YdbAreaDeviceEntity::getDeviceType, entity.getDeviceType())
                .eqIfPresent(YdbAreaDeviceEntity::getDeviceId, entity.getDeviceId())
                ;
    }


    @Select("select * from ydb_device_type_dict where code in(\n" +
            "select dev_type from ydb_device_type dt where exists(\n" +
            "select device_id from ydb_area_device ad  where dt.id=ad.device_id and  area_id =#{areaId}" +
            "))")
      List<YdbDeviceTypeDictEntity> selectByareaId(@Param("areaId") Long areaId);


     @Select("select * from ydb_device_type dt where exists(\n" +
             "select device_id from ydb_area_device ad  where dt.id=ad.device_id and  area_id =#{areaId} and dev_type = #{devType}   \n" +
             ")  ")
     List<YdbDeviceTypeEntity> selectDeviceALlByAreaId(@Param("areaId") Long areaId, @Param("devType") Integer devType);




}
